<?php


if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class plugin_yy_pay {
	var $is_limit = false;
	var $lang=null;
	var $cvar=null;
	function plugin_yy_pay(){		
		global $_G;
		$this->cvar= $_G['cache']['plugin']['yy_pay'];
		if(in_array($_G['groupid'], (array)unserialize($this->cvar['p_groups']))){
			$this->is_limit = true;
		}else{
			$this->is_limit = false;
		}
		$this->load_setting();
	}
	public function discuzcode() {
		global $_G;
		$message = $_G['discuzcodemessage'];
		if($_GET['mod'] !='viewthread')
			$message = preg_replace("/\[".$this->cvar['p_code']."(=([\d+]+?)|)\]\s*(.*?)\s*((\[\/".$this->cvar['p_code']."\])|\.\.\.)/is", lang('plugin/yy_pay','var15'), $message);
		$_G['discuzcodemessage'] = $message;
	}
	function _killpost(){
		global $_G;
		if(!$this->cvar['p_open'])
			return;
		if($_SERVER['REQUEST_METHOD']=='POST'){
			if(preg_match("/\[".$this->cvar['p_code']."(=([\d+]+?)|)\]\s*(.*?)\s*\[\/".$this->cvar['p_code']."\]/is",$_GET['message'],$matches))
				if(!in_array($_G['groupid'], (array)unserialize($this->cvar['p_allow'])))
					showmessage(lang('plugin/yy_pay','var29').$this->cvar['p_code']);				
		}
	}
	function load_setting(){
		global $_G;
		if(isset($_G['load_yyplugin']['yy_pay']['pro']))
			return;
		if($_G['setting']['version']=='X2.5')
			$dircache='sysdata';
		else
			$dircache='cache';
		if(file_exists(DISCUZ_ROOT.'./data/'.$dircache.'/cache_yy_pay_pro.php'))
			require_once DISCUZ_ROOT.'./data/'.$dircache.'/cache_yy_pay_pro.php';
		$_G['load_yyplugin']['yy_pay']['pro']=$pay_pro;
	}
}
class plugin_yy_pay_forum extends plugin_yy_pay{
	function post_killpost() {
		$this->_killpost();
	}
	function viewthread_postbottom_output() {
		global $_G, $postlist,$post;
		if(!$this->cvar['p_open']||!in_array($_G['fid'], (array)unserialize($this->cvar['p_forums'])))
			return array();
		if(empty($_GET['viewpid'])){
			foreach($postlist as $id => $p) {
				$postlist[$id] = $this->discuzcodeprase($p);
			}
		}else{
			$post=$this->discuzcodeprase($post);
		}
	}
	function discuzcodeprase($post){
		global $_G;
		$message = $post['message'];
		$pid = $post['pid'];
		$sell=array();
		$gjsd=$_G['load_yyplugin']['yy_pay']['pro'][$post['groupid']];
		preg_match_all("/\[".$this->cvar['p_code']."(=([\d+]+?)|)\]\s*(.*?)\s*\[\/".$this->cvar['p_code']."\]/is",$message,$matches);
		if(!count($matches[3]))
			return $post;
		if(in_array($post['groupid'], (array)unserialize($this->cvar['p_allow']))){
			$query=DB::query("SELECT * FROM ".DB::table("yy_pay")." WHERE p_pid=$pid");
			while($r = DB::fetch($query)) {
				$sell[$r['p_cid']]=array(
					'authorid'=>$r['p_authorid'],
					'extcredit'=>$r['p_extcredit'],
					'pay'=>$r['p_pay'],
				);
			}
			$query=DB::query("SELECT * FROM ".DB::table("yy_pay_log")." WHERE pl_pid=$pid and pl_uid=".$_G['uid']);
			while($r = DB::fetch($query)) {
				$sell[$r['pl_cid']]['display']=1;
			}
			for($i=0;$i<count($matches[3]);$i++){
				if($this->cvar['p_num']&&$i>=$this->cvar['p_num'])
					break;
				if($this->is_limit||$post['authorid']==$_G['uid'])
					$sell[$i]['display']=1;
				$pay=$matches[2][$i]?$matches[2][$i]:$this->cvar['p_credits'];
				if($pay>$this->cvar['p_max']&&!isset($gjsd['max']))
					$pay=$this->cvar['p_max'];
				if($pay>$gjsd['max']&&isset($gjsd['max']))
					$pay=$gjsd['max'];
				if(!$pay)
					continue;
				if(empty($sell[$i]))
					DB::query("INSERT INTO ".DB::table("yy_pay")."(p_pid,p_cid,p_pay,p_authorid,p_extcredit) VALUES('$pid','$i','$pay','$post[authorid]','".$this->cvar['p_credit']."')");
				elseif($sell[$i]['pay']!=$pay||$sell[$i]['extcredit']!=$this->cvar['p_credit'])
					DB::query("UPDATE ".DB::table("yy_pay")." SET p_pay='$pay',p_extcredit='".$this->cvar['p_credit']."' WHERE p_pid=$pid and p_cid=$i");
				if($sell[$i]['display']==1){
					$message = preg_replace("/\[".$this->cvar['p_code']."(=([\d+]+?)|)\]\s*(.*?)\s*\[\/".$this->cvar['p_code']."\]/is", $this->pay_reply($pid,$i), $message,1);
				} else{
					$message = preg_replace("/\[".$this->cvar['p_code']."(=([\d+]+?)|)\]\s*(.*?)\s*\[\/".$this->cvar['p_code']."\]/is", $this->pay_reply_hidden($pid,$post['authorid'],$pay,$i), $message,1);
				}
			}
		}
		$message = preg_replace("/\[".$this->cvar['p_code']."(=([\d+]+?)|)\]\s*(.*?)\s*\[\/".$this->cvar['p_code']."\]/is", '\\3', $message);
		$post['message']=$message;
		return $post;
	}
	function pay_reply($pid,$i){
		global $_G;
		return '<div class="showhide"><h4>'.lang('plugin/yy_pay','var1').'      <a href="javascript:;" title="'.lang('plugin/yy_pay','var14').'" onclick="showWindow(\'yy_payview\', \'plugin.php?id=yy_pay:payview&pid='.$pid.'&cid='.$i.'\')">'.lang('plugin/yy_pay','var14').'</a> </h4>\\3</div>';
	}
	function pay_reply_hidden($pid,$authorid,$pay,$i){
		global $_G;
		$str = '<div class="locked">';
		$str = $str .($_G['uid']?$_G['username']:lang('forum/template', 'guest'));
		$str = $str .','.lang('plugin/yy_pay','var2').' <strong>'.$pay.$_G['setting']['extcredits'][$this->cvar['p_credit']]['unit'].$_G['setting']['extcredits'][$this->cvar['p_credit']]['title'].'</strong> '.lang('plugin/yy_pay','var3').'&nbsp;&nbsp;&nbsp;'.$this->cvar['p_ad1'];
		$str = $str .'<a href="javascript:;" class="y viewpay" title="'.lang('plugin/yy_pay','var4').'" onclick="showWindow(\'yy_pay\', \'plugin.php?id=yy_pay:pay&pid='.$pid.'&authorid='.$authorid.'&cid='.$i.'\')">'.lang('plugin/yy_pay','var4').'</a></div>';
		return $str;
	}
	function post_editorctrl_left() {
	   	global $_G;
		if(!$this->cvar['p_open']||!in_array($_G['groupid'], (array)unserialize($this->cvar['p_allow']))||!in_array($_G['fid'], (array)unserialize($this->cvar['p_forums'])))
			return ;
		return "<link rel=\"stylesheet\" href=\"source/plugin/yy_pay/img/btn_pay.css\" type=\"text/css\" /><script src=\"plugin.php?id=yy_pay:addpay\" type=\"text/javascript\"></script><a id=\"btn_pay\" title=\"".lang('plugin/yy_pay','var5')."\" onClick=\"addpay('btn_pay')\" href='javascript:void(0);' >".lang('plugin/yy_pay','var6')."</a>";
	}
	
}
class mobileplugin_yy_pay{
	public function discuzcode() {
		global $_G;
		$cvar= $_G['cache']['plugin']['yy_pay'];
		$message = $_G['discuzcodemessage'];
		$message = preg_replace("/\[".$cvar['p_code']."(=([\d+]+?)|)\]\s*(.*?)\s*\[\/".$cvar['p_code']."\]/is", lang('plugin/yy_pay','var15'), $message);
		$_G['discuzcodemessage'] = $message;
	}
}
?>